﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Domain;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;
using SMTC.Model.Model;

namespace SMTC.Dao.Plant
{
    public class PlantProccessDao : BaseDao
    {
        /// <summary>
        /// 插入工厂信息
        /// </summary>
        private const string SQL_INSERT_PLANT = @"INSERT dbo.SMT_Plant (
	                                                    PlantID,
	                                                    PlantName,
	                                                    ModifyPerson,
	                                                    ModifyDateTime,
	                                                    PlantCapacity
                                                    ) VALUES ( 
	                                                    /* PlantID - uniqueidentifier */ @PlantID,
	                                                    /* PlantName - nvarchar(50) */ @PlantName,
	                                                    /* ModifyPerson - nvarchar(50) */ @ModifyPerson,
	                                                    /* ModifyDateTime - datetime */ GETDATE(),
	                                                    /* PlantCapacity - int */ 0 ) ";
        /// <summary>
        /// 插入工厂与bu的关系
        /// </summary>
        private const string SQL_INSERT_PLANT_BU = @"UPDATE dbo.SMT_BU SET PlantID = @PlantID
                                                    WHERE BUID = @BUID";
        /// <summary>
        /// 删除工厂信息
        /// </summary>
        private const string SQL_DELETE_PLANT = @"DELETE dbo.SMT_Plant WHERE PlantID = @PlantID";
        /// <summary>
        /// 删除工厂和bu的关系
        /// </summary>
        private const string SQL_DELETE_PLANT_BU = @"UPDATE dbo.SMT_BU SET PlantID = NULL WHERE PlantID = @PlantID";
        /// <summary>
        /// 更新工厂信息
        /// </summary>
        private const string SQL_UPDATE_PLANT = @"UPDATE dbo.SMT_Plant
                                                    SET PlantName = @PlantName
                                                    ,ModifyPerson = @ModifyPerson
                                                    ,ModifyDateTime = GETDATE()
                                                    ,PlantCapacity = @PlantCapacity
                                                    WHERE PlantID = @PlantID";
        /// <summary>
        /// 插入工厂信息
        /// </summary>
        /// <param name="plant"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool InsertPlant(SMT_PlantModel plant, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@PlantID",plant.PlantID)
               ,DBparameterBuild.Build("@PlantName",plant.PlantName)
               ,DBparameterBuild.Build("@ModifyPerson",plant.ModifyPerson)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_PLANT, parameters, dbt) > 0;
        }
        /// <summary>
        /// 插入工厂与bu的关系
        /// </summary>
        /// <param name="bu"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool InsertPlantsBU(SMT_BUModel bu, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@PlantID",bu.PlantID)
               ,DBparameterBuild.Build("@BUID",bu.BUID)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_PLANT_BU, parameters, dbt) > 0;
        }
        /// <summary>
        /// 删除工厂信息
        /// </summary>
        /// <param name="bu"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool DeletePlant(string PlantID, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@PlantID",PlantID)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_PLANT, parameters, dbt) > 0;
        }
        /// <summary>
        /// 删除工厂与bu的关系
        /// </summary>
        /// <param name="bu"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool DeletePlantsBU(string PlantID, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@PlantID",PlantID)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_PLANT_BU, parameters, dbt) > 0;
        }
        /// <summary>
        /// 删除工厂与bu的关系
        /// </summary>
        /// <param name="bu"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool UpdatePlant(SMT_PlantModel plant, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@PlantID",plant.PlantID)
               ,DBparameterBuild.Build("@PlantName",plant.PlantName)
               ,DBparameterBuild.Build("@ModifyPerson",plant.ModifyPerson)
               ,DBparameterBuild.Build("@PlantCapacity",plant.PlantCapacity)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_UPDATE_PLANT, parameters, dbt) > 0;
        }

    }
}
